﻿using M;
using M.Migration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Migrations;

namespace EfClient
{

    internal class OneToOneContextFactory : IDesignTimeDbContextFactory<OneToOneContext>
    {
        public OneToOneContext CreateDbContext(string[] args)
        {
            // 此处写死，简单一点，读取配置文件比较复杂，只是为了迁移数据库
            string connStr = "Host=127.0.0.1;Port=5432;Database=efsimple;User ID=postgres;Password=sa*123;Pooling=true;MinPoolSize=10;MaxPoolSize=50;";
            DbContextOptionsBuilder<OneToOneContext> builder = new();
            builder.UseNpgsql(connStr, 
                b => b.MigrationsAssembly("MClient")  // 这个必须有
                );
            // 实体更新时，取消外键约束。
            builder.ReplaceService<IMigrationsModelDiffer, MigrationsModelDifferWithoutForeignKey>();

            OneToOneContext ctx = new OneToOneContext(builder.Options);
            return ctx;
        }
    } 
}
